package com.luyuan.crm.member.web.converter;

import javax.annotation.Resource;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

import org.springframework.stereotype.Component;

import com.luyuan.crm.member.model.MemberGroup;
import com.luyuan.crm.member.service.MemberGroupService;
import com.ruijie.util.StringUtils;

@Component("memberGroupConverter")
public class MemberGroupConverter implements Converter {
    @Resource(name = "memberGroupService")
    private MemberGroupService memberGroupService;

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        if (StringUtils.isNullOrEmpty(value)) {
            return null;
        }
        return memberGroupService.getById(value);
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        MemberGroup memberGroup = (MemberGroup) value;
        if (memberGroup == null) {
            return null;
        }
        return memberGroup.getId();
    }

}
